﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace A2DFramework.ValidationService.RuleValidators
{
    public class RuleMobileAttribute : RuleValidationAttribute
    {
        public RuleMobileAttribute()
        { 
        }

        public RuleMobileAttribute(string rule):base(rule)
        {
        }


        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            string mobile = string.Empty;
            if (value != null)
                mobile = (string)value;
            mobile = mobile.Trim();

            if (mobile.Length == 0)
                return ValidationResult.Success;

            string regPattern = @"^(130|131|132|133|134|135|136|137|138|139)\d{8}$";
            if(!Regex.IsMatch(mobile, regPattern))
                return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));

            return ValidationResult.Success;
        }
    }
}
